﻿using System;
using System.Collections;
using System.Data;
using System.Windows.Forms;
using AMS.Inventory.PDA.Castle.DataAccess;

namespace AMS.Inventory.PDA
{
    public partial class FrmLogin : Form
    {
        private Hashtable hashTable;
        public bool IsLongin = false;
        public SqliteDBServer SqlDb = new SqliteDBServer();
        public string UserName = "";

        public FrmLogin()
        {
            InitializeComponent();
            hashTable = clsLocalization.ReadContectResource(Name, "");
            clsLocalization.getControlNames(this); 
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            DataTable userInfoDt = null;
            UserName = txtUserName.Text.Trim();
            if (UserName == "")
            {
                MessageBox.Show(clsLocalization.getFromContentNames(hashTable, "UserEmpty"));
                return;
            }
            if (txtPassword.Text.Trim() == "")
            {
                MessageBox.Show(clsLocalization.getFromContentNames(hashTable, "PasswordEmpty"));
                return;
            }

            userInfoDt = SqlDb.GetDataTable("SELECT  code as Name,Password FROM Frame_User where code='" + UserName + "'");
            string password = "";
            if (userInfoDt != null && userInfoDt.Rows.Count > 0)
            {
                password = userInfoDt.Rows[0]["Password"].ToString();
                if (password == txtPassword.Text.Trim())
                {
                    //UserName = txtUserName.Text.Trim();
                    IsLongin = true;
                    Close();
                }
                else
                {
                    MessageBox.Show(clsLocalization.getFromContentNames(hashTable, "PassworError"));
                    return;
                }
            }
            else
            {
                MessageBox.Show(clsLocalization.getFromContentNames(hashTable, "UserError"));
                return;
            }
        }

        private void BtnExist_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes ==
                 MessageBox.Show(clsLocalization.getFromContentNames(hashTable, "IsExistSys"), ModCommon.MagboxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                 MessageBoxDefaultButton.Button1))

            {
                SqlDb.ConnectedDbClose();
                Application.Exit();
            }
            else
            {
                MinimizeBox = false;
                Show();
            }
        }
    }
}